
<template>
  <div class="register_container">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
      <el-card>
        <div slot="header" class="clearfix">
          <span>用户注册</span>
        </div>
        <el-form-item label="用户名" prop="username">
          <el-input placeholder="请输入用户名（至少一位非数字）" v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input type="password" placeholder="请输入8-16位密码" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" placeholder="请再次输入密码" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item label="手机号" prop="phoneNumber">
          <el-input  placeholder="请输入手机号" v-model="ruleForm.phoneNumber" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="验证码" prop="verification">
          <el-input
              placeholder="请输入验证码"
              v-model="ruleForm.verification"
              clearable>
            <el-button slot="append" @click = "verifyCodeClick">获取验证码</el-button>
          </el-input>
        </el-form-item>

        <el-form-item class="button">
          <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-card>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else if(value.length<8 || value.length>16){
        callback(new Error('请输入8-16位密码'));
      } else{
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    var validatePass3 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入手机号'));
      } else if (value.length !=11 || !(/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/).test(value)) {
        callback(new Error('请输入正确手机号'));
      } else {
        callback();
      }
    };
    var validatePass4 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'));
      } else if((/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/).test(value)){
        callback(new Error('请至少包含一位非数字'));
      } else{
        callback();
      }
    };
    return {
      ruleForm: {
        password: '',
        checkPass: '',
        username: '',
        phoneNumber: '',
        verification: '',
      },
      rules: {
        password: [
          { min: 8, max: 16, validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        phoneNumber: [
          { len: 11, validator: validatePass3, trigger: 'blur' }
        ],
        username: [
          { validator: validatePass4, trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //调用axios发送请求
          this.registerClick();

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //用户注册
    registerClick(){
      this.$http({
        url:"http://47.104.100.60:82/registerCommonUser",
        method:"post",
        data:{
          username:this.ruleForm.username,
          password:this.ruleForm.password,
          iphone:this.ruleForm.phoneNumber,
          verifyCode:this.ruleForm.verification
        }
      }).then(res =>{
        //  根据返回值判断手机号和用户名是否已经注册，如果已经注册，alert警告
        //  如果没有注册，直接切换回到登录界面
        if('1'== res.data.success){
        //  跳转到登录界面
          this.$router.push({
            path:"/login"
          })
        }else if('0' == res.data.success){
          alert("电话号已经注册")
        }else {
          alert('注册失败,验证码错误')
        }
      }).catch(err=> {
        console.log(err)
      })
    },

    verifyCodeClick(){
      this.$http({
        url:"http://47.104.100.60:82/sendVerifyCode",
        method:"post",
        data:{
          iphone:this.ruleForm.phoneNumber
        }
      }).then(res => {
        //  alert展示验证码已经发送
        if (res.data[0].message == 'send success'){
          alert("发送成功")
        }else{
          alert("手机号不正确")
        }
      }).catch(err=>{
        alert("发送失败")
        console.log(err)
      })
    },
  }
}
</script>

<style>

.register_container{
  margin-top:180px;
  width: 30%;
  margin-left: 35%;
}

.button{
  margin-right: 10%;
}
</style>